const glob = require('glob')
const HtmlWebpackPlugin = require('html-webpack-plugin');
//获取所有入口函数
exports.fetchEntries = (path)=>{
  const entryFiles = glob.sync(path+"/*/index.ts");
  let entry = {};
  entryFiles.forEach(file => {
    const key = file.replace(/\/index\.ts$/i,"").split("/").pop();
		entry[key] = file;
  });
  return entry;
}

//有特殊配置的页面
const pageParams = {
  register:{
    needVuex:true
  },
  login:{
    needVuex:true
  },
  main:{
    needVuex:true
  }
}
exports.fetchHtmls = (entries,NODE_ENV,projectName)=>{
  let pages = [];
  Object.getOwnPropertyNames(entries).forEach(page=>{
    if(NODE_ENV=='development'){
      //开发状态
      pages.push(
        new HtmlWebpackPlugin({
          filename: `pages/${page}.html`,
          template: './src/index.dev.ejs',
          title:projectName,
          inject: true,
          chunks:[page],
          hash:true,
          chunksSortMode: function (chunk1, chunk2) {
              return chunk1.names[0]=="extjs"?-1:1;  
          },
          ...(pageParams[page]||{})
        })
      );
    }else{
      //TODO:打成发布包
      pages.push(
        new HtmlWebpackPlugin({
          filename: `pages/${page}.html`,
          template: './src/index.ejs',
          title:projectName,
          inject: true,
          chunks:[page],
          hash:true,
          chunksSortMode: function (chunk1, chunk2) {
              return chunk1.names[0]=="extjs"?-1:1;  
          },
          ...(pageParams[page]||{})
        })
      );

    }
  });
  return pages;
}